package com.tangyanbo.filemanager.icon;

import java.util.HashMap;
import java.util.Map;
import android.app.ListActivity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import com.tangyanbo.android.FileManager.R;

public class FileIcon {

	private final int unknowEndingIcon = R.drawable.file;
	private final int folderIcon = R.drawable.folder3;	
	private final Map<String,Integer> endingIconMap = new HashMap<String,Integer>();
	private ListActivity context;
	
	public FileIcon(ListActivity context){
		this.context = context;
		endingIconMap.put(".png", R.drawable.image);
		endingIconMap.put(".gif", R.drawable.image);
		endingIconMap.put(".jpg", R.drawable.image);
		endingIconMap.put(".jpeg", R.drawable.image);
		endingIconMap.put(".bmp", R.drawable.image);
		
		endingIconMap.put(".mp3", R.drawable.audio);
		endingIconMap.put(".wav", R.drawable.audio);
		endingIconMap.put(".ogg", R.drawable.audio);
		endingIconMap.put(".midi", R.drawable.audio);
		endingIconMap.put(".wma", R.drawable.audio);
		
		endingIconMap.put(".jar", R.drawable.packed);
		endingIconMap.put(".zip", R.drawable.packed);
		endingIconMap.put(".rar", R.drawable.packed);
		endingIconMap.put(".gz", R.drawable.packed);
		
		endingIconMap.put(".htm", R.drawable.webtext);
		endingIconMap.put(".html", R.drawable.webtext);
		endingIconMap.put(".php", R.drawable.webtext);
		
		endingIconMap.put(".mp4", R.drawable.video);
		endingIconMap.put(".rm", R.drawable.video);
		endingIconMap.put(".mpg", R.drawable.video);
		endingIconMap.put(".avi", R.drawable.video);
		endingIconMap.put(".mpeg", R.drawable.video);
		
		endingIconMap.put(".xml", R.drawable.xml);
	}
	
	private int getIconIdByFileEnding(String ending){
		Integer iconId = endingIconMap.get(ending);
		if(iconId==null){
			return unknowEndingIcon;
		}
		return iconId;
	}
	
	
	public Drawable getIconByFileEnding(String ending){		
		int iconId = getIconIdByFileEnding(ending);
		return context.getResources().getDrawable(iconId);
	}
	
	
	
	public Drawable getFolderIcon(){
		return context.getResources().getDrawable(folderIcon);
	}
	
	public Drawable getApkFileIcon(String filePath){
		return getAppInfoData(filePath).icon;
	}
	
	private AppInfoData getAppInfoData(String filePath){
		PackageManager packageManager = context.getPackageManager(); 
		PackageInfo info = packageManager.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
		AppInfoData appInfoData = new AppInfoData();
		if(info != null){ 
            ApplicationInfo appInfo = info.applicationInfo;
            appInfoData.appName = packageManager.getApplicationLabel(appInfo).toString(); 
            appInfoData.packageName = appInfo.packageName;
            appInfoData.icon = packageManager.getApplicationIcon(appInfo);          
        }
		return appInfoData;
	}
	
}
